package com.wolfking.converter.event;

import com.wolfking.converter.entity.SqlDefineParam;
import com.wolfking.converter.service.SqlDefineParamService;
import com.wolfking.jeesite.entity.SignEvent;
import com.wolfking.jeesite.listener.AbstractSignEventListener;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class SqlDefineParamEventListener extends AbstractSignEventListener<SqlDefineParamEvent> {

    @Autowired
    private SqlDefineParamService sqlDefineParamService;

    @Override
    public void onApplicationEvent(SqlDefineParamEvent event) {
        SignEvent signEvent = event.convert2SignEvent();
        String defineKey = event.getParameters().getOrDefault("defineKey", "").toString();
        if (StringUtils.isNoneBlank(defineKey)) {
            signEvent.setUrl("change_sql_param_" + defineKey);
            try {
                signEvent.setEvent(Class.forName(signEvent.getEvent()).getSimpleName());
            } catch (Exception e) {
                logger.warn("e", e);
            }
        } else {
            try {
                SqlDefineParam id = sqlDefineParamService.getById(Integer.parseInt(event.getParameters().get("id").toString()));
                signEvent.setUrl("change_sql_param_" + id.getDefineKey());
                signEvent.setEvent(Class.forName(signEvent.getEvent()).getSimpleName());
            } catch (Exception e) {
                logger.warn("e", e);
            }
        }
        saveSysSignEvent(signEvent);
    }
}
